<?php
/**
 * CustomerFormView
 *
 * @version    1.0
 * @package    samples
 * @subpackage tutor
 * @author     Pablo Dall'Oglio
 * @copyright  Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
 * @license    http://www.adianti.com.br/framework-license
 */
class CustomerFormView extends TPage
{
    private $form; // form
    
    /**
     * Class constructor
     * Creates the page and the registration form
     */
    function __construct()
    {
        parent::__construct();
        // creates the form
        $this->form = new TForm('form_customer');
        
        // creates a table
        $table_data    = new TTable;
        $table_contact = new TTable;
        $table_skill   = new TTable;
        
        $notebook = new TNotebook(500, 250);
        // add the notebook inside the form
        $this->form->add($notebook);
        
        $notebook->appendPage('Registration Data', $table_data);
        $notebook->appendPage('Contact (composition)', $table_contact);
        $notebook->appendPage('Skill (aggregation)', $table_skill);
        
        // create the form fields
        $code           = new TEntry('id');
        $name           = new TEntry('name');
        $address        = new TEntry('address');
        $phone          = new TEntry('phone');
        $city_id        = new TSeekButton('city_id');
        $city_name      = new TEntry('city_name');
        $birthdate      = new TDate('birthdate');
        $email          = new TEntry('email');
        $gender         = new TRadioGroup('gender');
        $status         = new TCombo('status');
        $contacts_list  = new TMultiField('contacts_list');
        $category_id    = new TDBCombo('category_id', 'samples', 'Category', 'id', 'name');
        
        $obj = new CitySeek;
        $city_id->setAction(new TAction(array($obj, 'onReload')));
        
        $itemGender = array();
        $itemGender['M'] = 'Male';
        $itemGender['F'] = 'Female';
        // add the combo options
        $gender->addItems($itemGender);
        $gender->setLayout('horizontal');
        
        $itemStatus= array();
        $itemStatus['S'] = 'Single';
        $itemStatus['C'] = 'Committed';
        $itemStatus['M'] = 'Married';
        $status->addItems($itemStatus);
        
        // define some properties for the form fields
        $code->setEditable(FALSE);
        $code->setSize(100);
        $city_id->setSize(80);
        $city_name->setSize(150);
        $city_name->setEditable(FALSE);
        $name->setSize(320);
        $address->setSize(320);
        $phone->setSize(120);
        $email->setSize(160);
        $birthdate->setSize(90);
        $status->setSize(120);
        $category_id->setSize(120);
        
        // add a row for the field code
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Code:'));
        $row->addCell($code);
        
        // add a row for the field name
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Name:'));
        $cell = $row->addCell($name);
        $cell->colspan=3;
        
        // add a row for the field address
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Address:'));
        $cell = $row->addCell($address);
        $cell->colspan=3;
        
        // add a row for the field city
        $row=$table_data->addRow();
        $row->addCell(new TLabel('City:'));
        $cell = $row->addCell($city_id);
        
        // add a row for the field name
        $row->addCell(new TLabel('Name:'));
        $row->addCell($city_name);
        
        // add a row for the field Phone
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Phone:'));
        $row->addCell($phone);
        
        // add a row for the field BirthDate
        $row->addCell(new TLabel('BirthDate:'));
        $row->addCell($birthdate);
        
        // add a row for the field status
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Status:'));
        $cell = $row->addCell($status);
        
        // add a row for the field Email
        $row->addCell(new TLabel('Email:'));
        $cell = $row->addCell($email);
        
        // add a row for the field Category
        $row=$table_data->addRow();
        $row->addCell(new TLabel('Category:'));
        $cell = $row->addCell($category_id);

        // add a row for the field gender
        $row->addCell(new TLabel('Gender:'));
        $row->addCell($gender);
        
        $row=$table_contact->addRow();
        $cell=$row->addCell(new TLabel('<b>Contact</b>'));
        $cell->valign = 'top';
        
        // add two fields inside the multifield in the second sheet
        $contacts_list->setHeight(100);
        $contacts_list->setClass('Contact'); // define the returning class
        $contacts_list->addField('type',  'Contact Type: ',  new TEntry('type'), 200);
        $contacts_list->addField('value', 'Contact Value: ', new TEntry('value'), 200);
        $row=$table_contact->addRow();
        $row->addCell($contacts_list);
        
        // create the radio button for the skills list
        $skill_list = new TDBCheckGroup('skill_list', 'samples', 'Skill', 'id', 'name');
        $table_skill->addRow()->addCell($lbl=new TLabel('Skills'));
        $table_skill->addRow()->addCell($skill_list);
        $lbl->setFontStyle('b');
        
        // create an action button
        $button1=new TButton('action1');
        $button1->setAction(new TAction(array($this, 'onSave')), 'Save');
        $button1->setImage('ico_save.png');
        
        // create an action button (go to list)
        $button2=new TButton('list');
        $button2->setAction(new TAction(array('CustomerDataGridView', 'onReload')), 'Go to Listing');
        $button2->setImage('ico_datagrid.gif');
        
        // define wich are the form fields
        $this->form->setFields(array($code, $name, $address, $phone, $city_id, $city_name,
                                     $birthdate, $email, $gender, $status, $category_id,
                                     $contacts_list, $skill_list, $button1, $button2));
        
        $subtable = new TTable;
        $row = $subtable->addRow();
        $row->addCell($button1);
        $row->addCell($button2);
        
        $table_layout = new TTable;
        $table_layout->addRow()->addCell($this->form);
        $table_layout->addRow()->addCell($subtable);
        
        // add the form inside the page
        parent::add($table_layout);
    }
    
    /**
     * method onSave
     * Executed whenever the user clicks at the save button
     */
    function onSave()
    {
        try
        {
            // open a transaction with database 'samples'
            TTransaction::open('samples');
            
            // read the form data and instantiates an Active Record
            $customer = $this->form->getData('Customer');
            
            if ($customer->contacts_list)
            {
                foreach ($customer->contacts_list as $contact)
                {
                    // add the contact to the customer
                    $customer->addContact($contact);
                }
            }
            
            if ($customer->skill_list)
            {
                foreach ($customer->skill_list as $skill_id)
                {
                    // add the skill to the customer
                    $customer->addSkill(new Skill($skill_id));
                }
            }
            // stores the object in the database
            $customer->store();
            $this->form->setData($customer);
            
            // shows the success message
            new TMessage('info', 'Record saved');
            
            TTransaction::close(); // close the transaction
        }
        catch (Exception $e) // in case of exception
        {
            // shows the exception error message
            new TMessage('error', '<b>Error</b>' . $e->getMessage());
            // undo all pending operations
            TTransaction::rollback();
        }
    }
    
    /**
     * method onEdit
     * Edit a record data
     */
    function onEdit($param)
    {
        try
        {
            if (isset($param['key']))
            {
                // open a transaction with database 'samples'
                TTransaction::open('samples');
                
                // load the Active Record according to its ID
                $customer= new Customer($param['key']);
                
                // load the contacts (composition)
                $customer->contacts_list = $customer->getContacts();
                
                // load the skills (aggregation)
                $skills = $customer->getSkills();
                $skill_list = array();
                if ($skills)
                {
                    foreach ($skills as $skill)
                    {
                        $skill_list[] = $skill->id;
                    }
                }
                $customer->skill_list = $skill_list;
                
                // fill the form with the active record data
                $this->form->setData($customer);
                
                // close the transaction
                TTransaction::close();
            }
            else
            {
                $this->form->clear();
            }
        }
        catch (Exception $e) // in case of exception
        {
            // shows the exception error message
            new TMessage('error', '<b>Error</b>' . $e->getMessage());
            // undo all pending operations
            TTransaction::rollback();
        }
    }
}
?>